<?php 
namespace plugin;
class Index extends \Plugin {

	public function index(){
		if (\Ebcms::isPost()) {
			$captcha = \mylib\Input::post('captcha');
			if (!$captcha || $captcha != \mylib\Session::get('captcha')) {
				return $this -> error('验证码不正确！');
			}
			\mylib\Session::delete('captcha');
			\Ebcms::db() -> insert('guestbook', [
				'contact'=>\mylib\Input::post('contact'),
				'nickname'=>\mylib\Input::post('nickname'),
				'body'=>\mylib\Input::post('body'),
				'create_time'=>time()
			]);
			return $this -> success('留言成功！');
		}else{
			return $this -> html($this -> fetch());
		}
	}

	public function captcha(){
		$num = mt_rand(1, 50);
		$max = mt_rand($num, $num + 50);
		\mylib\Session::set('captcha', $num);
		return \mylib\Response::image(\Captcha::create(($max-$num).'+?='.$max, 16));
	}
}